package com.qianchang.microfinance.view.credit.data;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.bigkoo.pickerview.model.IPickerViewData;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DateUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.OneLineEditLinearLayout;
import com.dm.library.widgets.OneLineLinearLayout;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.BasicStudentBean;
import com.qianchang.microfinance.bean.CommonSinglePickBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.utils.GetJsonDataUtil;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.dialog.AreaDialog;
import com.qianchang.microfinance.view.dialog.CommonDialog;
import com.qianchang.microfinance.view.dialog.DateDialog;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述:  学生贷 基本信息
 * 修 改 人: zhoulai
 * 修改时间: 2017/8/16 15:48
 */


public class BasicStudentActivity extends BaseActivity implements OneLineEditLinearLayout.TextWatcher {


    private static final String TAG = "BasicStudentActivity";

    @BindView(R.id.oll_colleges_area)
    OneLineLinearLayout ollCollegesArea;
    @BindView(R.id.oll_major)
    OneLineEditLinearLayout ollMajor;
    @BindView(R.id.oll_Education)
    OneLineLinearLayout ollEducation;
    @BindView(R.id.oll_entrance_time)
    OneLineLinearLayout ollEntranceTime;
    @BindView(R.id.oll_graduation_time)
    OneLineLinearLayout ollGraduationTime;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    @BindView(R.id.oll_colleges_name)
    OneLineEditLinearLayout ollCollegesName;

    private AreaDialog areaDialog;
    private String mProvinceId;
    private String mCityId;
    private String mAreaId;
    private String majorName;
    private String collegeName;
    private String education;
    private String entranceTime;
    private String graduationTime;
    private String currentId;
    private String projectType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_student);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        ollMajor.setTextWatcher(this, 1);
        ollCollegesName.setTextWatcher(this, 2);
        setTitleName("基本信息");
    }

    private void getBasicInfo() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_STUDENT_INFO, params, new RequestCallBack<BasicStudentBean>() {
            @Override
            public void onSuccess(JsonResult<BasicStudentBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    BasicStudentBean basicStudentBean = response.getObject();
                    if (null != basicStudentBean) {
                        refreshUi(basicStudentBean);
                    }
                } else {
                    ToastUtil.getInstant().show(BasicStudentActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(BasicStudentActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }
        });

    }

    private void refreshUi(BasicStudentBean basicStudentBean) {
//        btnCommit.setVisibility(View.GONE);
//        ViewUtils.disableSubControls(llContent);
        mProvinceId = basicStudentBean.getProvinceId();
        mCityId = basicStudentBean.getCityId();
        mAreaId = basicStudentBean.getCountryId();
        education = basicStudentBean.getEduBackgroud();
        entranceTime = basicStudentBean.getEntranceTime();
        graduationTime = basicStudentBean.getGraduationTime();
        majorName = basicStudentBean.getMajorName();
        collegeName = basicStudentBean.getCollegeName();

        String princeAndCity = GetJsonDataUtil.newInstance().getPrinceAndCityById(mProvinceId, mCityId);
        String area = GetJsonDataUtil.newInstance().getAreaById(mAreaId);
        ollCollegesArea.setRightContent(princeAndCity + area);

        ollEducation.setRightContent(education);
        ollEntranceTime.setRightContent(entranceTime);
        ollGraduationTime.setRightContent(graduationTime);
        ollMajor.setRightContent(majorName);
        ollCollegesName.setRightContent(collegeName);
    }


    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if (null != bundle) {
            currentId = bundle.getString(ConfirmInfoJinbaoActivity.CURRENT_ID);
            projectType = bundle.getString(ConstantAppInfo.PROJECT_TYPE);
        }
        getBasicInfo();
    }

    @OnClick({R.id.oll_colleges_area, R.id.oll_Education, R.id.oll_entrance_time, R.id.oll_graduation_time, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.oll_colleges_area://学院地区  选择
                if (areaDialog == null) {
                    areaDialog = new AreaDialog(this);
                    areaDialog.setOnAreaSelectListener(new AreaDialog.OnAreaSelectListener() {
                        @Override
                        public void onAreaSelect(String provinceId, String cityId, String areaId, String result) {
                            mProvinceId = provinceId;
                            mCityId = cityId;
                            mAreaId = areaId;
                            ollCollegesArea.setRightContent(result);
                        }
                    });
                }
                areaDialog.showPickerView();
                break;
            case R.id.oll_Education://学历 选择
                CommonDialog.Builder builder = CommonDialog.init()
                        .setOnItemSelectListener(new CommonDialog.OnItemSelectListener() {
                            @Override
                            public void onItemSelect(IPickerViewData iPickerViewData, int position, int flag) {
                                CommonSinglePickBean educationBean = (CommonSinglePickBean) iPickerViewData;
                                education = educationBean.getName();
                                ollEducation.setRightContent(education);
                            }
                        })
                        .setContext(this);
                builder.setData(BasicInitDataUtils.getEducationList(this))
                        .setTitle(getString(R.string.education));
                builder.build().showPickerView();

                break;
            case R.id.oll_entrance_time://入学时间
                DateDialog dateDialogEntrance = new DateDialog(BasicStudentActivity.this);
                dateDialogEntrance.setTitle("日期选择");
/*                dateDialogEntrance.setStartDate(2011, 2, 3);
                dateDialogEntrance.setEndDate(2017, 3, 22);*/
                dateDialogEntrance.setOnDateSelectListener(new DateDialog.OnDateSelectListener() {
                    @Override
                    public void onDateSelect(Date date) {
                        entranceTime = DateUtil.date2String(date);
                        ollEntranceTime.setRightContent(entranceTime);
                    }
                });
                dateDialogEntrance.showPickerView();
                break;
            case R.id.oll_graduation_time://毕业时间
                DateDialog dateDialogGraduation = new DateDialog(BasicStudentActivity.this);
                dateDialogGraduation.setTitle("日期选择");
/*                dateDialogEntrance.setStartDate(2011, 2, 3);
                dateDialogEntrance.setEndDate(2017, 3, 22);*/
                dateDialogGraduation.setOnDateSelectListener(new DateDialog.OnDateSelectListener() {
                    @Override
                    public void onDateSelect(Date date) {
                        graduationTime = DateUtil.date2String(date);
                        ollGraduationTime.setRightContent(graduationTime);
                    }
                });
                dateDialogGraduation.showPickerView();
                break;
            case R.id.btn_commit:
                submit();
                break;
        }
    }

    private void submit() {
        if (TextUtils.isEmpty(mProvinceId)
                || TextUtils.isEmpty(collegeName)
                || TextUtils.isEmpty(entranceTime)
                || TextUtils.isEmpty(majorName)
                || TextUtils.isEmpty(graduationTime)
                || TextUtils.isEmpty(education)) {
            ToastUtil.getInstant().show(BasicStudentActivity.this, getString(R.string.msg_finish_basic_info));
            return;
        }
        show();
        HttpParams params = new HttpParams();
        params.put("provinceId", mProvinceId);
        params.put("cityId", mCityId);
        params.put("countryId", mAreaId);
        params.put("collegeName", collegeName);
        params.put("majorName", majorName);
        params.put("eduBackgroud", education);
        params.put("entranceTime", entranceTime);
        params.put("graduationTime", graduationTime);
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.INSERT_STUDENT_INFO, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(JsonResult<String> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    BasicStudentActivity.this.finish();
                } else {
                    ToastUtil.getInstant().show(BasicStudentActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(BasicStudentActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }

        });
    }

    @Override
    public void afterTextChanged(Editable s, int flag) {
        switch (flag) {
            case 1://major
                majorName = s.toString().trim();
                break;
            case 2://ollCollegesName
                collegeName = s.toString().trim();
                break;
        }
    }
}
